package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.business.domain.bo.BedsBo;
import com.ruoyi.business.domain.vo.BedsVo;
import com.ruoyi.business.domain.Beds;
import com.ruoyi.business.mapper.BedsMapper;
import com.ruoyi.business.service.IBedsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 床位Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-25
 */
@RequiredArgsConstructor
@Service
public class BedsServiceImpl implements IBedsService {

    private final BedsMapper baseMapper;

    /**
     * 查询床位
     */
    @Override
    public BedsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询床位列表
     */
    @Override
    public TableDataInfo<BedsVo> queryPageList(BedsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Beds> lqw = buildQueryWrapper(bo);
        Page<BedsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询床位列表
     */
    @Override
    public List<BedsVo> queryList(BedsBo bo) {
        LambdaQueryWrapper<Beds> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Beds> buildQueryWrapper(BedsBo bo) {
        LambdaQueryWrapper<Beds> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBedcode()), Beds::getBedcode, bo.getBedcode());
        lqw.eq(bo.getEmployeeId() != null, Beds::getEmployeeId, bo.getEmployeeId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Beds::getStatus, bo.getStatus());
        lqw.eq(bo.getDormitoryId()!=null, Beds::getDormitoryId, bo.getDormitoryId());
        return lqw;
    }

    /**
     * 新增床位
     */
    @Override
    public Boolean insertByBo(BedsBo bo) {
        Beds add = BeanUtil.toBean(bo, Beds.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改床位
     */
    @Override
    public Boolean updateByBo(BedsBo bo) {
        Beds update = BeanUtil.toBean(bo, Beds.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Beds entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除床位
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
    /**
     * 批量新增床位
     */
    @Override
    public Boolean insertByBoBatch(List<Beds> bo) {
        boolean flag = baseMapper.insertBatch(bo);
        return flag;
    }

}
